package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.NaviGoodsService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import java.util.HashSet;
import java.util.List;

/**
 * @author Alone
 * @version 1.0
 * @date 2020/7/10 16:41
 */
@Component
@Service
@Slf4j
public class NaviGoodsServiceImpl implements NaviGoodsService {
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;

    @Override
    public NavListResponse naviList() {
        NavListResponse response = new NavListResponse();

        try{
            //导航栏的项目panel_id 为 0
            Example example = new Example(PanelContent.class);
            //此处的 panelId 必须是实体类的变量名 不可以写作panel_id
            example.createCriteria().andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            //利用converter转化为DTO
            List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
            response.setPannelContentDtos(panelContentDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("NaviGoodsServiceImpl.naviList occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    @Override
    public AllProductResponse queryGoods(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try{
            String orderCol="updated";
            String orderDir="desc";
            if(request.getSort().equals("1")){
                orderCol="price";
                orderDir="asc";
            }else if(request.getSort().equals("-1")){
                orderCol="price";
                orderDir="desc";
            }
            PageHelper.startPage(request.getPage(),request.getSize());
            List<Item> items = itemMapper.selectItemFront(request.getCid(), orderCol, orderDir, request.getPriceGt(), request.getPriceLte());
            List<ProductDto> productDtos = productConverter.items2Dto(items);
            response.setProductDtoList(productDtos);
            PageInfo<Item> info = new PageInfo<>(items);
            response.setTotal(info.getTotal());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("NaviGoodsServiceImpl.queryGoods occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    @Override
    public RecommendResponse queryRecommend() {
        RecommendResponse response = new RecommendResponse();
        try{
            List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            HashSet<PanelDto> panelDtos = new HashSet<>();
            if(panels!=null){
                for (Panel panel:panels) {
                    //查出热门推荐对应的 panel content item
                    List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                    List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                    //将 panel 转化为 dto
                    PanelDto panelDto = contentConverter.panen2Dto(panel);
                    panelDto.setPanelContentItems(panelContentItemDtos);
                    panelDtos.add(panelDto);
                }
            }
            response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("NaviGoodsServiceImpl.queryRecommend occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
